package com.newcoin.chat.dao;

import com.newcoin.chat.beans.entity.FriendApplicationEntity;
import com.newcoin.chat.beans.entity.FriendMsgEntity;
import com.newcoin.chat.beans.vo.FriendOfflineMsgVo;
import com.newcoin.chat.beans.vo.PageParamVo;
import com.newcoin.chat.beans.vo.UserVo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface FriendDao {
    /**
     * 通过账号后者昵称搜索好友(添加好友)
     *
     * @param p
     * @return
     */
    @Select("SELECT id,account,nickname,sex,province,city,area_county AS areaCounty,head_img_path AS headImgPath FROM chat_user WHERE account LIKE #{p} OR nickname LIKE #{p}")
    List<UserVo> getFriendInfoByFriendNicknameOrAccount(@Param("p") String p);

    /**
     * 验证是否已是好友
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Select("SELECT COUNT(*) FROM friend_relationship WHERE user_id=#{userId}  AND friend_id=#{friendId}")
    int verifyThatYouAreAlreadyFriends(@Param("userId") long userId, @Param("friendId") long friendId);

    /**
     * 新增好友申请
     *
     * @param entity
     * @return
     */
    int addFriendApplicationInfo(FriendApplicationEntity entity);

    /**
     * 是否有新的好友申请消息
     *
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM friend_application WHERE to_user=#{userId} AND state=0")
    int isThereANewFriendApplication(@Param("userId") long userId);

    /**
     * 获取最新10条的好友申请列表
     *
     * @param userId
     * @return
     */
    List<FriendApplicationEntity> getFriendApplicationListById(@Param("userId") long userId);

    /**
     * 根据申请信息id获取申请信息
     *
     * @param FriendApplication
     * @return
     */
    @Select("SELECT id,from_user AS fromUser,to_user AS toUser,state,remark FROM friend_application WHERE id=#{friendApplicationId}")
    FriendApplicationEntity getFriendApplicationById(@Param("friendApplicationId") long FriendApplication);

    /**
     * 同意或者拒绝好友申请/标记已读
     *
     * @param friendApplicationId
     * @param state
     * @return
     */
    @Update("UPDATE friend_application SET state=#{state} WHERE id=#{friendApplicationId}")
    int updateFriendApplicationStateById(@Param("friendApplicationId") long friendApplicationId, @Param("state") int state);

    /**
     * 添加好友
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Insert("INSERT INTO friend_relationship (user_id,friend_id) VALUES (#{userId},#{friendId})")
    int addFriend(@Param("userId") long userId, @Param("friendId") long friendId);

    /**
     * 删除好友
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Delete("DELETE FROM friend_relationship WHERE user_id=#{userId} AND friend_id=#{friendId}")
    int deleteFriend(@Param("userId") long userId, @Param("friendId") long friendId);

    /**
     * 打开或关闭阅后即焚
     *
     * @param userId
     * @param friendId
     * @param state
     * @return
     */
    @Update("UPDATE friend_relationship SET is_open_burn_down=#{state} WHERE user_id=#{userId} AND friend_id=#{friendId}")
    int openOrCloseAfterReadingIncineration(@Param("userId") long userId, @Param("friendId") long friendId, @Param("state") int state);

    /**
     * 新增新系统消息
     *
     * @param friendMsgEntity
     * @return
     */
    @Insert("INSERT INTO friend_msg (from_user,to_user,type,content,is_have_read,state,is_system_type,create_timestamp) VALUES (#{fromUser},#{toUser},#{type},#{content},#{isHaveRead},#{state},#{isSystemType},UNIX_TIMESTAMP(NOW()))")
    int addNewSystemMessages(FriendMsgEntity friendMsgEntity);

    /**
     * 获取所有的好友申请信息
     *
     * @param pageParamVo
     * @return
     */
    List<FriendApplicationEntity> getAllFriendApplicationListById(PageParamVo pageParamVo);

    /**
     * 获取好友申请总条数
     *
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM friend_application WHERE to_user=#{userId}")
    int getAllFriendApplicationCount(@Param("userId") long userId);

    /**
     * 通讯录列表
     *
     * @param userId
     * @return
     */
    @Select("SELECT a.id,a.nickname,a.head_img_path AS headImgPath FROM chat_user a,friend_relationship b WHERE a.id=b.friend_id AND b.user_id=#{userId} ORDER BY b.id ASC")
    List<UserVo> getContactList(@Param("userId") long userId);

    /**
     * 获取离线消息
     *
     * @param userId
     * @return
     */
    @Select("select unread_message_num,last_message_id from friend_relationship where user_id=#{userId} and friend_id=#{friendId} and unread_message_num>0 ")
    Map<String, Long> getFriendOfflineMsg(@Param("userId") long userId, @Param("friendId") long friendId);

    /**
     * 获取与好友的聊天
     *
     * @param userId
     * @return
     */
    @Select("")
    List<FriendMsgEntity> getFriendMsg(@Param("userId") long userId, @Param("friendId") long friendId, @Param("page") long page);

    /**
     * 根据id获取用户信息
     *
     * @param userId
     * @return
     */
//    @Select("SELECT id,account,nickname,sex,province,city,area_county AS areaCounty,head_img_path AS headImgPath FROM chat_user WHERE id=#{userId}")
    UserVo getUserInfoById(@Param("userId") long userId);


    /**
     * 获取与朋友的阅后即焚状态
     *
     * @param userId
     * @param friendId
     * @return
     */
    Integer getAfterReadingIncinerationState(@Param("userId") long userId, @Param("friendId") long friendId);

    /**
     * 分页获取离线消息
     *
     * @param id
     * @return
     */
    List<FriendOfflineMsgVo> getFriendOfflineMsgVo(@Param("id") long id,@Param("userId") Long userId, @Param("friendId") long friendId);

    /**
     * 清空未读消息条数
     *
     * @param userId
     * @param friendId
     * @return
     */
    int updateOfflineMsg(@Param("userId") long userId, @Param("friendId") long friendId);

    /**
     * 修改未读为已读
     * @param id
     * @return
     */
    int updateMsgIsHaveReadStatus(@Param("id") Long id);

}
